#ifndef _ITEM_H_
#define _ITEM_H_

#include "animation.h"
#include "player.h"
#include "score.h"

extern Score score;
extern Atlas atlas_apple;
extern Atlas atlas_apple_eaten;
bool is_eaten = false;
extern Atlas atlas_flag_on;
extern Atlas atlas_flag_being_on;
extern Atlas atlas_flag_idle;
extern Player* player;

class Item
{
public:
	Item() = default;
	~Item() = default;
	Item(int x, int y,Atlas*bef,Atlas*aft)
	{
		set_position(x, y);
		animation.set_atlas(bef);
		eaten.set_atlas(aft);
		eaten.set_interval(50);
		animation.set_interval(50);
		eaten.set_loop(false);
		current_animation = &animation;
		flag = false;
	}
	Item(int x, int y, Atlas* bef, Atlas* aft, Atlas* aftaft)
	{
		set_position(x, y);
		animation.set_atlas(bef);
		eaten.set_atlas(aft);
		eaten.set_interval(50);
		animation.set_interval(50);
		eaten.set_loop(false);
		current_animation = &animation;
		aft_eaten.set_atlas(aftaft);
		aft_eaten.set_interval(50);
		flag = true;
	}

	void set_position(int x, int y)
	{
		position.x = x;
		position.y = y;
		
	}

	bool is_have_eaten()
	{
		if (current_animation == &eaten&&current_animation->check_finished())
		{
			
			return true;
		}
		return false;
	}
	

	void on_draw(const Camera& camera) const
	{
		current_animation->on_draw(camera, position.x, position.y);

	}

	void on_update(int delta)
	{
		if (!(current_animation == &eaten)&& !(current_animation == &aft_eaten)&&player->get_hp()>0) {
		
			if (player->get_position().x <= position.x + (current_animation->get_frame()->getwidth())/2 && player->get_position().x + 64 >= position.x + (current_animation->get_frame()->getwidth()) / 2 && player->get_position().y + 64 >= position.y + (current_animation->get_frame()->getheight()) / 2 && player->get_position().y <= position.y + (current_animation->get_frame()->getheight()) / 2)
			{
				
				mciSendString(_T("stop bgm_coin"), NULL, 0, NULL);
				mciSendString(_T("seek bgm_coin to start"), NULL, 0, NULL);
				mciSendString(_T("play bgm_coin"), NULL, 0, NULL);
				if(!flag) score.getpoint(1);
				current_animation = &eaten;
				/*
				if (player->get_hp() < 100)
				{
					player->set_hp(player->get_hp() + 10);
				}
				*/
			}
			
		}

		if (is_have_eaten() && flag)
		{
			score.getpoint(3);
			current_animation = &aft_eaten;
		}

		current_animation->on_update(delta);
	}
	void exit() {
		animation.reset();
		eaten.reset();
		aft_eaten.reset();
		current_animation = nullptr;
	}


private:
	Animation animation;
	Animation eaten;
	Animation aft_eaten;
	Vector2 position;
	Animation* current_animation;
	bool flag;
};













#endif // !_ITEM_H_

